import 'package:flutter/material.dart';

/// 创建两个页面
/// 1. 使用Navigator.push跳转第二个页面
/// 2. 使用Navigator.pop返回第一个页面

class NavigatorScreenApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final String appTitle = 'Navigator Screen Demo';
    return MaterialApp(
      title: appTitle,
      theme: ThemeData(primarySwatch: Colors.red),
      home: NavigatorScreenAppPage(title: appTitle),
    );
  }
}

class NavigatorScreenAppPage extends StatefulWidget {
  final String title;

  NavigatorScreenAppPage({Key key, this.title}) : super(key: key);

  @override
  State<StatefulWidget> createState() => NavigatorScreenAppPageState();
}

class NavigatorScreenAppPageState extends State<NavigatorScreenAppPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: RaisedButton(
            child: Text('Launch screen'),
            onPressed: () {
              // 开启新页面
              Navigator.push(context,
                  MaterialPageRoute(builder: (context) => SecondScreen()));
            }),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Second Screen'),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            // 回退
            Navigator.pop(context);
          },
          child: Text('Go back'),
        ),
      ),
    );
  }
}
